import java.util.Arrays;

/**
 * @ClassName KP38_
 * @Description: TODO
 * @Author: JCC
 * @CreateTime: 2023/11/1 20:49
 */
public class KP37_MergeSort {
    // 归并排序递归方式
    public static void mergeSortRecursion(int[] arr) {
        if (arr == null || arr.length < 2) {
            return;
        }
        mergeSortRecursionChild(arr, 0, arr.length - 1);
    }

    public static void mergeSortRecursionChild(int[] arr, int L, int R) {
        if (L == R) {
            return;
        }
        int mid = L + ((R - L) >>> 2);
        mergeSortRecursionChild(arr, L, mid);
        mergeSortRecursionChild(arr, mid + 1, R);
        merge(arr, L, mid, R);
    }

    public static void merge(int[] arr, int L, int mid, int R) {
        int[] newArray = new int[R - L + 1];
        int i = 0;
        int pLeft = L;
        int pRight = mid + 1;
        while (pLeft <= mid && pRight <= R) {
            newArray[i++] = arr[pLeft] <= arr[pRight] ? arr[pLeft++] : arr[pRight++];
        }
        while (pLeft <= mid) {
            newArray[i++] = arr[pLeft++];
        }
        while (pRight <= R) {
            newArray[i++] = arr[pRight++];
        }
        for (int k = 0; k < newArray.length; k++) {
            arr[L + k] = newArray[k];
        }
    }

    /**
     * 归并排序非递归方式
     *
     * @param arr
     */
    public static void mergeSortNonRecursive(int[] arr) {
        if (arr == null || arr.length < 2) {
            return;
        }
        int step = 1;
        int len = arr.length;
        while (step < len) {
            int L = 0;
            while (L < len) {
                if (len - L <= step) {
                    break;
                }
                int M = L + step - 1;
                // len - M - 1 --> len - 1 - (M + 1) + 1
                // len - M - 1 --> len - L -step
                int R = M + Math.min(step, len - M - 1);
                merge(arr, L, M, R);
                L = R + 1;
            }
            if (step > len / 2) {
                break;
            }
            step <<= 1;
        }
    }


    // 判断两个数组是否相同
    public static boolean isEqual(int[] arr1, int[] arr2) {
        if (arr1 == null && arr2 == null) {
            return true;
        }
        if (arr1 == null ^ arr2 == null || arr1.length != arr2.length) {
            return false;
        }
        for (int i = 0; i < arr1.length; i++) {
            if (arr1[i] != arr2[i]) {
                return false;
            }
        }
        return true;
    }

    // 打印数组
    public static void printArray(int[] arr) {
        if (arr == null) {
            return;
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();
    }

    // 生成随机数组
    public static int[] generateRandomArray(int maxSize, int maxValue) {
        int[] arr = new int[(int) ((maxSize + 1) * Math.random())];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = (int) ((maxValue + 1) * Math.random()) - (int) (maxValue * Math.random());
        }
        return arr;
    }

    // for test
    public static void main(String[] args) {
        int testTime = 500000;
        int maxSize = 100;
        int maxValue = 100;
        System.out.println("测试开始");
        for (int i = 0; i < testTime; i++) {
            int[] arr1 = generateRandomArray(maxSize, maxValue);
            int[] arr2 = Arrays.copyOf(arr1, arr1.length);
            //mergeSortRecursion(arr1);
            mergeSortNonRecursive(arr1);
            Arrays.sort(arr2);
            if (!isEqual(arr1, arr2)) {
                System.out.println("出错了！");
                printArray(arr1);
                printArray(arr2);
                break;
            }
        }
        System.out.println("测试结束");
    }

}
